package com.ks.xcamera.manager;

import com.ks.basictools.provider.ContextGlobal;
import com.ks.xcamera.constant.Constants;
import com.ks.xcamera.responseBean.CustomCityData;
import com.ks.xcamera.responseBean.StoreAndKuaiDi;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListDataManager {

    private final List<CustomCityData> threeDataList = new ArrayList<>();
    private final List<CustomCityData> storeDataList = new ArrayList<>();
    private final List<CustomCityData> kuaiDiDataList = new ArrayList<>();

    private static class ListDataManagerHolder{
        private static final ListDataManager instance = new ListDataManager();
    }

    public ListDataManager() {}

    public static ListDataManager getInstance() {
        return ListDataManagerHolder.instance;
    }

    public List<CustomCityData> getThreeDataList() {
        return threeDataList;
    }

    public List<CustomCityData> getStoreDataList() {
        return storeDataList;
    }

    public List<CustomCityData> getKuaiDiDataList() {
        return kuaiDiDataList;
    }

    private void setThreeDataList(List<CustomCityData> list) {
        threeDataList.clear();
        threeDataList.addAll(list);
    }

    private void setStoreDataList(List<CustomCityData> list) {
        storeDataList.clear();
        storeDataList.addAll(list);
    }

    private void setKuaiDiDataList(List<CustomCityData> list) {
        kuaiDiDataList.clear();
        kuaiDiDataList.addAll(list);
    }

    public void requestData() {
        requestThreeData();
    }

    private void requestThreeData() {
        Map<String, Object> param = new HashMap<>();
        param.put("token", PreferenceManager.getInstance().getToken(ContextGlobal.get().getContext()));
        XHttp.post(Constants.URL_GOODS_LIST)
                .params(param)
                .execute(new SimpleCallBack<List<CustomCityData>>() {

                    @Override
                    public void onSuccess(List<CustomCityData> beanList) {
                        setThreeDataList(beanList);
                        requestStoreAndKuaidiData();
                    }

                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showLongToast(ContextGlobal.get().getContext(), e.getMessage());
                    }
                });
    }

    private void requestStoreAndKuaidiData() {
        Map<String, Object> param = new HashMap<>();
        param.put("token", PreferenceManager.getInstance().getToken(ContextGlobal.get().getContext()));
        XHttp.post(Constants.URL_INDEX)
                .params(param)
                .execute(new SimpleCallBack<StoreAndKuaiDi>() {

                    @Override
                    public void onSuccess(StoreAndKuaiDi bean) {
                        setStoreDataList(bean.getShop_list());
                        setKuaiDiDataList(bean.getExpress_comp_list());
                    }

                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showLongToast(ContextGlobal.get().getContext(), e.getMessage());
                    }
                });
    }
}
